//
//  LaunchViewController.swift
//  ScarnKing
//
//  Created by ninebot on 2025/7/22.
//

import UIKit

class LaunchViewController: UIViewController {
    
    // MARK: - UI Components
    private let logoImageView = UIImageView()
    private let appNameLabel = UILabel()
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupConstraints()
        startLaunchAnimation()
    }
    
    // MARK: - UI Setup
    private func setupUI() {
        let isDark = ThemeManager.shared.isDarkMode
        view.backgroundColor = ThemeManager.Colors.background(for: isDark)
        
        // Logo
        logoImageView.image = UIImage(named: "about_icon")
        logoImageView.contentMode = .scaleAspectFit
        logoImageView.alpha = 0.0
        logoImageView.layer.cornerRadius = 20
        logoImageView.clipsToBounds = true
        
        // 添加阴影效果
        logoImageView.layer.shadowColor = UIColor.black.cgColor
        logoImageView.layer.shadowOffset = CGSize(width: 0, height: 3)
        logoImageView.layer.shadowOpacity = 0.6
        logoImageView.layer.shadowRadius = 6
        
        // App名称
        appNameLabel.text = "智能扫描仪"
        appNameLabel.font = UIFont.boldSystemFont(ofSize: 24)
        appNameLabel.textColor = UIColor.systemBlue
        appNameLabel.textAlignment = .center
        appNameLabel.alpha = 0.0
        
        // 添加阴影效果
        appNameLabel.layer.shadowColor = UIColor.systemBlue.withAlphaComponent(0.6).cgColor
        appNameLabel.layer.shadowOffset = CGSize(width: 0, height: 2)
        appNameLabel.layer.shadowOpacity = 0.6
        appNameLabel.layer.shadowRadius = 4
        
        view.addSubview(logoImageView)
        view.addSubview(appNameLabel)
    }
    
    private func setupConstraints() {
        logoImageView.translatesAutoresizingMaskIntoConstraints = false
        appNameLabel.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            // Logo居中
            logoImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            logoImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -50),
            logoImageView.widthAnchor.constraint(equalToConstant: 120),
            logoImageView.heightAnchor.constraint(equalToConstant: 120),
            
            // App名称在Logo下方
            appNameLabel.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 30),
            appNameLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            appNameLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            appNameLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
        ])
    }
    
    // MARK: - Animation
    private func startLaunchAnimation() {
        // Logo淡入动画
        UIView.animate(withDuration: 0.8, delay: 0.2, options: .curveEaseOut) {
            self.logoImageView.alpha = 1.0
        } completion: { _ in
            self.startLogoAnimation() // 开始重复动画
        }
        
        // App名称淡入动画
        UIView.animate(withDuration: 0.6, delay: 0.8, options: .curveEaseOut) {
            self.appNameLabel.alpha = 1.0
        }
        
        // 3秒后跳转到主应用
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            self.transitionToMainApp()
        }
    }
    
    private func startLogoAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.scale")
        animation.fromValue = 1.0
        animation.toValue = 1.1
        animation.duration = 0.6
        animation.autoreverses = true
        animation.repeatCount = 3
        animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        
        logoImageView.layer.add(animation, forKey: "pulse")
    }
    
    private func transitionToMainApp() {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = windowScene.windows.first else {
            return
        }
        
        let mainViewController = TabViewController()
        window.rootViewController = mainViewController
        
        UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: nil, completion: nil)
    }
} 